package com.hk.mybatis.query;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * 排序条件类
 */
@Getter
@Setter
public class OrderBy {

    public final static String ASC = "asc";

    public final static String DESC = "desc";

    /**
     * 排序属性
     */
    private String property;

    /**
     * 排序方式 asc或desc
     */
    private String direction;

    public OrderBy() {
    }

    /**
     * 构造方法
     *
     * @param property  {@link String} 排序属性
     * @param direction {@link String} 排序方式 asc或desc
     */
    public OrderBy(String property, String direction) {
        this.property = property;
        if (StringUtils.hasLength(direction)) {
            this.direction = direction;
        } else {
            this.direction = DESC;
        }
    }


    /**
     * 获取排序条件
     *
     * @return {@link OrderItem} 排序条件
     */
    public boolean isAsc() {
        return Objects.equals(this.direction, ASC);
    }

    /**
     * 获取排序条件
     *
     * @return {@link OrderItem} 排序条件
     */
    public OrderItem getOrderBy() {
        return new OrderItem(this.property, Objects.equals(this.direction, ASC));
    }
}